﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.Servidor1;

namespace WebApplication1
{
    public partial class Login : System.Web.UI.Page
    {
        Service1 servidor;
        bool ResultadoAutenticacion;
        string Empresa;

        protected void Page_Load(object sender, EventArgs e)
        {
            servidor = new Service1();
            Session["Server"] = servidor; 
            if (!Page.IsPostBack)
            {
                if (Session["Server"] != null)
                {
                    List<string> listOfNames = servidor.Consultar(1).ToList();
                    DropDownList1.DataSource = listOfNames;
                    DropDownList1.DataBind();
                }

             }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Empresa = DropDownList1.SelectedItem.Text;
            Label1.Text = Empresa;
            if ((TextBox1.Text != "") && (TextBox2.Text != ""))
            {
                ResultadoAutenticacion = servidor.Autenticar_Usuario(TextBox1.Text, TextBox2.Text, DropDownList1.SelectedItem.Text);
            }

            if (ResultadoAutenticacion == true)
            {
                Session.Add("NombreUsuario", TextBox1.Text);
                Session.Add("NombreEmpresaUsuario", Empresa);
                Response.Redirect("PaginaInicio.aspx");
            }
            else
            {
                Label1.Text = "Datos incorrectos. Por favor, verifíquelos.";
                Label1.Visible = true;
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList DropDownList1 = (DropDownList)sender;
            Empresa = DropDownList1.SelectedValue.ToString();
        }
    }
}